package com.whyx.project.openapi;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.whyx.project.openapi.config.CorsFilter;
import com.whyx.project.openapi.controller.ServiceController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.io.IOException;

@SpringBootApplication(scanBasePackages={"com.whyx.project.openapi"} ,exclude= {DataSourceAutoConfiguration.class})
@EnableSwagger2
public class ApplicationOpenApi {
    public static final String CURRENT_VERSION = "v2";
    public static final String COMPATIBLE_VERSION = "v2,v1";

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(
                        new ApiInfoBuilder()
                                .title("天空众创基础版本API")
                                .description("当前API版本" + CURRENT_VERSION + " 兼容API版本" + COMPATIBLE_VERSION)
                                .version(CURRENT_VERSION)
                                .build())
                .select().apis(RequestHandlerSelectors.basePackage(ApplicationOpenApi.class.getPackage().getName())).build();
    }
    @Bean
    public ObjectMapper objectMapper() {
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        ObjectMapper myObjectMapper = new ObjectMapper();
        myObjectMapper.registerModule(simpleModule);
        myObjectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        myObjectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        myObjectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        myObjectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        myObjectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
        {
            @Override
            public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
                jsonGenerator.writeString("");
            }
        });
        return myObjectMapper;
    }

    @Bean()
    public CorsFilter AuthFilter() {
        return new CorsFilter();
    }
    private static final Logger logger = LoggerFactory.getLogger(ApplicationOpenApi.class);

    public static void main(String[] args) {
        SpringApplication.run(ApplicationOpenApi.class, args);
//        ServiceController.getLocation();
//        ServiceController.getHistoryTrackList();
        logger.info(" 启动完成");
    }
}
